#include <PA9.h>
#include <stdlib.h>

extern u8 Config_LeftHanded;

//#define HeldTime 3
//#define HeldDelta 5

//int gtX[HeldTime], gtY[HeldTime], GHeld, GPressed;

void GTouchInit()
{
/*
	int i;
	for (i=0;i<HeldTime;i++)
	{
		gtX[i] = -1;
		gtY[i] = -1;
	}
	GHeld = GPressed = 0;
*/
}

u8 PadNewpress(char button)
{
   if (!Config_LeftHanded)
   {
      switch (button)
      {
         case 'L': return Pad.Newpress.L;
         case 'R': return Pad.Newpress.R;
         case 'u': return Pad.Newpress.Up;
         case 'd': return Pad.Newpress.Down;
         case 'l': return Pad.Newpress.Left;
         case 'r': return Pad.Newpress.Right;
         case 'A': return Pad.Newpress.A;
         case 'B': return Pad.Newpress.B;
         case 'X': return Pad.Newpress.X;
         case 'Y': return Pad.Newpress.Y;
      }   
   } else {
      switch (button)
      {
         case 'L': return Pad.Newpress.R;
         case 'R': return Pad.Newpress.L;
         case 'u': return Pad.Newpress.X;
         case 'd': return Pad.Newpress.B;
         case 'l': return Pad.Newpress.Y;
         case 'r': return Pad.Newpress.A;
         case 'A': return Pad.Newpress.Right;
         case 'B': return Pad.Newpress.Down;
         case 'X': return Pad.Newpress.Up;
         case 'Y': return Pad.Newpress.Left;
      }   
   }   
	return 0;  
}   

u8 PadHeld(char button)
{
   if (!Config_LeftHanded)
   {
      switch (button)
      {
         case 'L': return Pad.Held.L;
         case 'R': return Pad.Held.R;
         case 'u': return Pad.Held.Up;
         case 'd': return Pad.Held.Down;
         case 'l': return Pad.Held.Left;
         case 'r': return Pad.Held.Right;
         case 'A': return Pad.Held.A;
         case 'B': return Pad.Held.B;
         case 'X': return Pad.Held.X;
         case 'Y': return Pad.Held.Y;
      }   
   } else {
      switch (button)
      {
         case 'L': return Pad.Held.R;
         case 'R': return Pad.Held.L;
         case 'u': return Pad.Held.X;
         case 'd': return Pad.Held.B;
         case 'l': return Pad.Held.Y;
         case 'r': return Pad.Held.A;
         case 'A': return Pad.Held.Right;
         case 'B': return Pad.Held.Down;
         case 'X': return Pad.Held.Up;
         case 'Y': return Pad.Held.Left;
      }   
   }   
	return 0;  
}   

u8 PadReleased(char button)
{
   if (!Config_LeftHanded)
   {
      switch (button)
      {
         case 'L': return Pad.Released.L;
         case 'R': return Pad.Released.R;
         case 'u': return Pad.Released.Up;
         case 'd': return Pad.Released.Down;
         case 'l': return Pad.Released.Left;
         case 'r': return Pad.Released.Right;
         case 'A': return Pad.Released.A;
         case 'B': return Pad.Released.B;
         case 'X': return Pad.Released.X;
         case 'Y': return Pad.Released.Y;
      }   
   } else {
      switch (button)
      {
         case 'L': return Pad.Released.R;
         case 'R': return Pad.Released.L;
         case 'u': return Pad.Released.X;
         case 'd': return Pad.Released.B;
         case 'l': return Pad.Released.Y;
         case 'r': return Pad.Released.A;
         case 'A': return Pad.Released.Right;
         case 'B': return Pad.Released.Down;
         case 'X': return Pad.Released.Up;
         case 'Y': return Pad.Released.Left;
      }   
   }   
	return 0;  
}   

u8 GTouch(u8 noblock = 0)
{
/*
	int i;
	for (i=HeldTime-1;i>0;i--)
	{
		gtX[i] = gtX[i-1];
		gtY[i] = gtY[i-1];
	}
	if (Stylus.Held && (!Pad.Held.L || noblock))
	{
		gtX[0] = Stylus.X;
		gtY[0] = Stylus.Y;
		int GoodHeld = 1;
		for (i=1;i<HeldTime;i++)
		{
			if ((abs(gtX[0] - gtX[i]) > HeldDelta) &&
			  (abs(gtY[0] - gtY[i]) > HeldDelta)) GoodHeld = 0;
		}
		if ((!GPressed) && (GoodHeld))
		{
			GPressed = 1;
			return 1;
		}		
	} else {
		gtX[0] = -1;
		gtY[0] = -1;
		GPressed = 0;
	}
	return 0;
*/
	return (Stylus.Newpress && (!PadHeld('L') || noblock));
}

u8 GTouchStillHeld(u8 noblock = 0)
{
	return (Stylus.Held  && (!PadHeld('L') || noblock));
/*
	if (Stylus.Held && (!Pad.Held.L || noblock))
	{
		for (u8 i=1;i<HeldTime;i++)
		{
			if ((abs(gtX[0] - gtX[i]) > HeldDelta) &&
			  (abs(gtY[0] - gtY[i]) > HeldDelta)) return 0;
		}
		return 1;
	}
	return 0;
*/
}

/*
int GRectTouched(int X1, int Y1, int X2, int Y2) {
	if ((Stylus.X >= X1) && (Stylus.X <= X2) &&
	  (Stylus.Y >= Y1) && (Stylus.Y <= Y2))
	{
		return 1;
	} else {
		return 0;
	}
}
*/
